package com.lzj.web.controller.tool;

import cn.hutool.core.bean.BeanUtil;
import com.lzj.common.annotation.Log;
import com.lzj.common.annotation.RepeatSubmit;
import com.lzj.common.core.controller.BaseController;
import com.lzj.common.core.domain.PageQuery;
import com.lzj.common.core.domain.R;
import com.lzj.common.core.domain.entity.SysDictData;
import com.lzj.common.core.page.TableDataInfo;
import com.lzj.common.core.validate.AddGroup;
import com.lzj.common.core.validate.EditGroup;
import com.lzj.common.enums.BusinessType;
import com.lzj.common.utils.poi.ExcelUtil;
import com.lzj.system.domain.bo.SysComQueryBo;
import com.lzj.system.domain.vo.SysComQueryVo;
import com.lzj.system.service.ISysComQueryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 通用查询
 *
 * @author lzj
 * @date 2024-10-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tool/comQuery")
public class SysComQueryController extends BaseController {

    private final ISysComQueryService iSysComQueryService;

    /**
     * 查询通用查询列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysComQueryVo> list(SysComQueryBo bo, PageQuery pageQuery) {
        return iSysComQueryService.queryPageList(bo, pageQuery);
    }

    /**
     * 测试查询数据
     * @return List<SysDictData>
     */
    @PostMapping("/test")
    public List<SysDictData> testQueryBiz(@RequestBody SysComQueryBo bo) {
        return iSysComQueryService.selectQueryData(bo);
    }

    /**
     * 查询业务数据
     * 
     * @param code 查询码
     * @param params 携带参数
     * @return List<SysDictData>
     */
    @GetMapping("/data/{code}")
    public List<SysDictData> queryBizData(@PathVariable("code") String code, @RequestParam Map<String, Object> params) {

        SysComQueryBo queryBo = new SysComQueryBo();
        queryBo.setCode(code);

        List<SysComQueryVo> vos = iSysComQueryService.queryList(queryBo);
        if (vos == null || vos.isEmpty()) {
            return null;
        }
        SysComQueryVo sysComQueryVo = vos.get(0);
        queryBo = BeanUtil.toBean(sysComQueryVo, SysComQueryBo.class);
        queryBo.getParams().putAll(params);
        return iSysComQueryService.selectQueryData(queryBo);
    }

    /**
     * 导出通用查询列表
     */
    @Log(title = "通用查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysComQueryBo bo, HttpServletResponse response) {
        List<SysComQueryVo> list = iSysComQueryService.queryList(bo);
        ExcelUtil.exportExcel(list, "通用查询", SysComQueryVo.class, response);
    }
    
    /**
     * 获取通用查询详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public SysComQueryVo getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return iSysComQueryService.queryById(id);
    }

    /**
     * 新增通用查询
     */
    @Log(title = "通用查询", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysComQueryBo bo) {
        return toAjax(iSysComQueryService.insertByBo(bo));
    }

    /**
     * 修改通用查询
     */
    @Log(title = "通用查询", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysComQueryBo bo) {
        return toAjax(iSysComQueryService.updateByBo(bo));
    }

    /**
     * 删除通用查询
     *
     * @param ids 主键串
     */
    @Log(title = "通用查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysComQueryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
